package com.ruoyi.project.system.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.PaperQuestion;
import com.ruoyi.project.system.mapper.PaperQuestionMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.PaperMapper;
import com.ruoyi.project.system.domain.Paper;
import com.ruoyi.project.system.service.IPaperService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 组卷Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-19
 */
@Service
public class PaperServiceImpl implements IPaperService
{
    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private PaperQuestionMapper paperQuestionMapper;

    /**
     * 查询组卷
     *
     * @param id 组卷ID
     * @return 组卷
     */
    @Override
    public Paper selectPaperById(Long id)
    {
        List<PaperQuestion> paperQuestionList = paperQuestionMapper.selectPaperQuestionList(new PaperQuestion(){{
            setPaperId(id);
        }});
        List<Map<String,Object>> resp = new ArrayList<>();
        Paper paper = paperMapper.selectPaperById(id);
        for (PaperQuestion paperQuestion : paperQuestionList){
            Map<String,Object> map = new HashMap<>();
            map.put("id",paperQuestion.getId());
            map.put("paperId",paperQuestion.getPaperId());
            map.put("questionStoreId",paperQuestion.getQuestionStoreId());
            map.put("score",paperQuestion.getScore());
            resp.add(map);
        }
        paper.setList(resp);
        return paper;
    }

    /**
     * 查询组卷列表
     *
     * @param paper 组卷
     * @return 组卷
     */
    @Override
    public List<Paper> selectPaperList(Paper paper)
    {
        paper.setProgressCode(null);
        return paperMapper.selectPaperList(paper);
    }

    /**
     * 新增组卷
     *
     * @param paper 组卷
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPaper(Paper paper,LoginUser loginUser)
    {
        paper.setOperatorId(loginUser.getUser().getUserId());
        paper.setOperatorName(loginUser.getUser().getUserName());
        paper.setCreateDt(new Date());
        paper.setPublishDt(new Date());
        //判断题目数量
        if(paper.getList().size() > 0 ){
            paper.setQuestionCount( Long.parseLong(paper.getList().size()+""));
        }else{
            throw new CustomException("请至少选择一个题目作为试卷内容");
        }

        //判断总分是否大于合格分
        /**
         * int a = bigdemical.compareTo(bigdemical2)
         *
         * a = -1,表示bigdemical小于bigdemical2；
         *
         * a = 0,表示bigdemical等于bigdemical2；
         *
         * a = 1,表示bigdemical大于bigdemical2；
         */

        Double sum = paper.getList().stream().collect(Collectors.summingDouble(c -> Double.valueOf(c.get("score")+"")));
        paper.setScore(sum);
        //合格人数
        if(paper.getScore().compareTo(paper.getStandardScore())==-1 ){
            throw new CustomException("总分需大于合格分");
        }

        //判断小项分数之和是否等于总分

        if(paper.getScore().compareTo(sum)!=0){
            throw new CustomException("总分需等于各题目分数之和");
        }


        Integer i =  paperMapper.insertPaper(paper);

        //id  score:分数
        List<Map<String, Object>> list = paper.getList();
        for (Map<String, Object> map : list) {
            PaperQuestion paperQuestion = new PaperQuestion();
            paperQuestion.setPaperId(paper.getId());

            paperQuestion.setQuestionStoreId(Long.parseLong(map.get("questionStoreId")+""));

            paperQuestion.setScore(Double.valueOf(map.get("score")+""));
            paperQuestionMapper.insertPaperQuestion(paperQuestion);
        }

        return 1;
    }

    /**
     * 修改组卷
     *
     * @param paper 组卷
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePaper(Paper paper)
    {
        paperQuestionMapper.deletePaperQuestionByPaperId(paper.getId());

        //判断题目数量
        if( paper.getList().size() > 0 ){
            paper.setQuestionCount( Long.parseLong(paper.getList().size()+""));
        }else{
            throw new CustomException("请至少选择一个题目作为试卷内容");
        }

        Double sum = paper.getList().stream().collect(Collectors.summingDouble(c -> Double.valueOf(c.get("score")+"")));
        paper.setScore(sum);

        //判断小项分数之和是否等于总分
        paper.setScore(sum);

        //判断总分是否大于合格分
        /**
         * int a = bigdemical.compareTo(bigdemical2)
         *
         * a = -1,表示bigdemical小于bigdemical2；
         *
         * a = 0,表示bigdemical等于bigdemical2；
         *
         * a = 1,表示bigdemical大于bigdemical2；
         */

        //合格人数
        if(paper.getScore().compareTo(paper.getStandardScore())==-1){
            throw new CustomException("总分需大于合格分");
        }

        //判断小项分数之和是否等于总分

        if(paper.getScore().compareTo(sum)!=0){
            throw new CustomException("总分需等于各题目分数之和");
        }

        //id  score:分数
        List<Map<String, Object>> list = paper.getList();
        for (Map<String, Object> map : list) {
            PaperQuestion paperQuestion = new PaperQuestion();
            paperQuestion.setPaperId(paper.getId());

            paperQuestion.setQuestionStoreId(Long.parseLong(map.get("questionStoreId")+""));

            paperQuestion.setScore(Double.valueOf(map.get("score")+""));
            paperQuestionMapper.insertPaperQuestion(paperQuestion);
        }
        return paperMapper.updatePaper(paper);
    }

    /**
     * 批量删除组卷
     *
     * @param ids 需要删除的组卷ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePaperByIds(Long[] ids)
    {
        for(Long id :ids){
            paperQuestionMapper.deletePaperQuestionByPaperId(id);
        }
        return paperMapper.deletePaperByIds(ids);
    }

    /**
     * 删除组卷信息
     *
     * @param id 组卷ID
     * @return 结果
     */
    @Override
    public int deletePaperById(Long id)
    {
        return paperMapper.deletePaperById(id);
    }
}
